﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.Common;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using BOMS.Utility;

namespace BOMS.DBUtility
{
  public  class DbHelper
    {
      public static string DBType = ConfigurationManager.AppSettings["DBType"];

      public static DataTable Rt_Table(string str_Sqldt)
      {
          DataTable dt = new DataTable();
          if (DBType == ConstSet.DBTYPE_MYSQL)
          {
              dt = DbHelperMySql.Rt_Table(str_Sqldt);
          }
          else if (DBType == ConstSet.DBTYPE_MSSQL)
          {
              dt = DbHelperSQL.Rt_Table(str_Sqldt);
          }
          return dt;

      }

      public static DataSet Query(string SQLString)
      {
          DataSet ds = new DataSet();
          if (DBType == ConstSet.DBTYPE_MYSQL)
          {
              ds = DbHelperMySql.Query(SQLString);
          }
          else if (DBType == ConstSet.DBTYPE_MSSQL)
          {
              ds = DbHelperSQL.Query(SQLString);
          }
          return ds;
      }

      public static int ExecuteSql(string SQLString)
      {
          int count = 0;
          if (DBType == ConstSet.DBTYPE_MYSQL)
          {
              count = DbHelperMySql.ExecuteSql(SQLString);
          }
          else if (DBType == ConstSet.DBTYPE_MSSQL)
          {
              count = DbHelperSQL.ExecuteSql(SQLString);
          }
          return count;
      }
      public static int ExecuteSql(string SQLString,params DbParameter[] Parameters)
      {
          int count = 0;
          if (DBType == ConstSet.DBTYPE_MYSQL)
          {
              MySqlParameter[] MysqlPara = new MySqlParameter[Parameters.Length];
              for (int i = 0; i < MysqlPara.Length; i++)
              {
                  MysqlPara[i] = (MySqlParameter)Parameters[i];
              }
              count = DbHelperMySql.ExecuteSql(SQLString, MysqlPara);
          }
          else if (DBType == ConstSet.DBTYPE_MSSQL)
          {
              SqlParameter[] MssqlPara = new SqlParameter[Parameters.Length];
              for (int j = 0; j < MssqlPara.Length; j++)
              {
                  MssqlPara[j] = (SqlParameter)Parameters[j];
              }
              count = DbHelperSQL.ExecuteSql(SQLString, MssqlPara);
          }
          return count;
      }
      public static object GetSingle(string SQLString)
      {
          object obj = new object();
          if (DBType == ConstSet.DBTYPE_MYSQL)
          {
              obj = DbHelperMySql.GetSingle(SQLString);
          }
          else if (DBType == ConstSet.DBTYPE_MSSQL)
          {
              obj = DbHelperSQL.GetSingle(SQLString);
          }
          return obj;
      }

      /// <summary>
      /// 执行多条SQL语句，实现数据库事务。
      /// </summary>
      /// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SqlParameter[]）</param>
      public static int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList,List<ProcCommandInfo> procList=null)
      {
          int count = 0;
          if (DBType == ConstSet.DBTYPE_MYSQL)
          {
             // count = DbHelperMySql.ExecuteSqlTran(cmdList);
              count = DbHelperMySql.ExecuteSqlTran(cmdList, procList); //Modified liangning 2013/4/21.
          }
          else if (DBType == ConstSet.DBTYPE_MSSQL)
          {
              count= DbHelperSQL.ExecuteSqlTran(cmdList);
          }
          return count;
      }





    }
}
